package cn.yto.print.utils;

import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.WinReg;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

public class RegistryUtil {

    private static final Logger logger = LogManager.getLogger(RegistryUtil.class);

    private static final String SID_PREFIX = "S-1-5-21";

    /**
     * 获取注册表中以指定前缀开头的 SID 列表。
     *
     * @param keyPath 注册表路径
     */
    public static List<String> getSidsWithPrefix(String keyPath) {
        List<String> sids = new ArrayList<>();
        try {
            String[] subKeys = Advapi32Util.registryGetKeys(WinReg.HKEY_LOCAL_MACHINE, keyPath);
            for (String subKey : subKeys) {
                if (subKey.startsWith(SID_PREFIX)) {
                    sids.add(subKey);
                }
            }
        } catch (Exception e) {
            logger.error("获取注册表 SID 异常", e);
        }
        return sids;
    }

    public static void main(String[] args) {
        String keyPath = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList";
        List<String> sids = getSidsWithPrefix(keyPath);
        for (String sid : sids) {
            System.out.println("SID: " + sid);
        }
    }

}
